package ecarx.settings.view;


import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import ecarx.settings.R;

public class ShowModeDialogManager implements View.OnClickListener {

    // *************************TODO 属性*************************************

    private static final String TAG = ShowModeDialogManager.class.getSimpleName();
    private static ShowModeDialogManager instance;

    private WindowManager windowManager;
    private View dialogView;
    Button cancelButton;
    Button okButton;
    private boolean isDialogShown;

    private DialogButtonListener listener;

    // ************************TODO 构造函数************************************

    private ShowModeDialogManager() {
    }

    public synchronized static ShowModeDialogManager getInstance() {
        if (instance == null) {
            instance = new ShowModeDialogManager();
        }
        return instance;
    }

    // *************************TODO 操作***********************************


    public void setListener(DialogButtonListener listener) {
        this.listener = listener;
    }

    public void show(Context context) {

        if (!isDialogShown) {

            this.initDialogView(context.getApplicationContext());

            isDialogShown = true;
        }
    }

    public void dismiss() {
        if (isDialogShown) {
            if (dialogView != null && windowManager != null) {
                try {
                    windowManager.removeView(dialogView);
                } catch (IllegalArgumentException e) {
                    // 视图可能已被移除，忽略异常
                } finally {
                    windowManager = null;
                    dialogView = null;
                }
            }
            isDialogShown = false;
        }
    }

    public boolean isShown() {
        return this.isDialogShown;
    }

    private void initDialogView(Context context) {

        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        dialogView = LayoutInflater.from(context).inflate(R.layout.common_dialog, null);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 50,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);

        float width = context.getResources().getDimensionPixelSize(R.dimen.dimens778dp);
        float height = context.getResources().getDimensionPixelSize(R.dimen.dimens428dp);

        params.width = (int) width;
        params.height = (int) height;
        params.alpha = 1f;
        params.dimAmount = 0.5f;
        params.gravity = Gravity.CENTER;
        params.x = 0;
        params.y = 0;

        windowManager.addView(dialogView, params);

        cancelButton = dialogView.findViewById(R.id.operation_cancel_btn);
        cancelButton.setOnClickListener(this);

        okButton = dialogView.findViewById(R.id.operation_close_btn);
        okButton.setOnClickListener(this);
    }

    // ************************TODO button点击事件处理************************************

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.operation_cancel_btn) {
            if (this.listener != null) {
                this.listener.onCancelClicked();
            }
            this.dismiss();
        } else if (v.getId() == R.id.operation_close_btn) {
            if (this.listener != null) {
                this.listener.onOkClicked();
            }
            this.dismiss();
        }
    }

    // ***********************TODO Button的自定义点击事件钩子Listener*************************************

    public interface DialogButtonListener {

        void onCancelClicked();

        void onOkClicked();
    }
}
